package com.ruoyi.article.controller;

import com.ruoyi.article.domain.Classify;
import com.ruoyi.article.service.ClassifyService;
import com.ruoyi.common.core.constant.ClassifyConstants;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysDept;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/classify")
public class ClassifyController extends BaseController {

    @Autowired
    private ClassifyService classifyService;


    @GetMapping("/hello")
    public String Hello(){
        return "Hello World 123";
    }


    @GetMapping("/list")
    public  AjaxResult list(Classify classify) {

        List<Classify> list = classifyService.selectClassifyList(classify);
        return success(list);
    }

    /**
     * 查询部门列表（排除节点）
     */
//    @RequiresPermissions("system:classify:list")
    @GetMapping("/list/exclude/{classifyId}")
    public AjaxResult excludeChild(@PathVariable(value = "classifyId", required = false) Long classifyId)
    {
        List<Classify> classifies = classifyService.selectClassifyList(new Classify());
        classifies.removeIf(c -> c.getClassifyId().intValue() == classifyId || ArrayUtils.contains(StringUtils.split(c.getAncestors(), ","), classifyId + ""));
        return success(classifies);
    }


    /**
     * 根据分类编号获取详细信息
     */
//    @RequiresPermissions("classify:classify:query")
    @GetMapping(value = { "/{classifyId}" })
    public AjaxResult getInfo(@PathVariable(value = "classifyId", required = false) Long classifyId) {
        return success(classifyService.selectClassifyById(classifyId));
    }

    /**
     * 新增类型
     */
    @Log(title = "分类管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Classify classify) {

        if (!classifyService.checkClassifyUnique(classify)) {
            return error("新增分类'" + classify.getClassifyId() + "'失败，分类已存在");
        }
        classify.setCreateBy(SecurityUtils.getUsername());
        return toAjax(classifyService.insertClassify(classify));
    }


//    @Log(title = "分类管理", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@Validated @RequestBody Classify classify) {
//        return toAjax(classifyService.updateClassify(classify));
//    }
//
//    @Log(title = "分类管理", businessType = BusinessType.DELETE)
//    @DeleteMapping("/{classifyId}")
//    public AjaxResult remove(@PathVariable Long classifyId) {
//        return toAjax(classifyService.deleteClassifyById(classifyId));
//    }

    /**
     * 修改类型
     */
//    @RequiresPermissions("classify:classify:edit")
    @Log(title = "分类管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Classify classify)
    {
        Long classifyId = classify.getClassifyId();
//        classifyService.checkClassifyDataScope(classifyId);
//        if (!classifyService.checkClassifyUnique(classify))
//        {
//            return error("修改分类'" + classify.getName() + "'失败，分类名称已存在");
//        }
//        else
        if (classify.getParentId().equals(classifyId))
        {
            return error("修改分类'" + classify.getName() + "'失败，上级分类不能是自己");
        }
        else if (StringUtils.equals(ClassifyConstants.CLASSIFY_DISABLE, classify.getStatus()) && classifyService.selectNormalChildrenClassifyById(classifyId) > 0)
        {
            return error("该分类包含未停用的子分类！");
        }
        classify.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(classifyService.updateClassify(classify));
    }

    /**
     * 删除分类
     */
//    @RequiresPermissions("classify:classify:remove")
    @Log(title = "分类管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{classifyId}")
    public AjaxResult remove(@PathVariable Long classifyId)
    {
        if (classifyService.hasChildByClassifyId(classifyId))
        {
            return warn("存在下级分类，不允许删除");
        }
        if (classifyService.checkClassifyExistRecord(classifyId))
        {
            return warn("分类存在记录，不允许删除");
        }
//        classifyService.checkClassifyDataScope(classifyId);
        return toAjax(classifyService.deleteClassifyById(classifyId));
    }




    @Log(title = "分类管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Classify classify) {
        List<Classify> list = classifyService.selectClassifyList(classify);
        ExcelUtil<Classify> util = new ExcelUtil<Classify>(Classify.class);
        util.exportExcel(response, list, "分类数据");
    }


}
